package com.xinnet.mms.allot.service.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.allot.dao.CustomAllotConfigDao;
import com.xinnet.mms.allot.service.CustomAllotConfigService;
import com.xinnet.mms.entity.CustomAllotConfig;
import com.xinnet.mms.entity.CustomAllotConfigDept;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.utils.CustomerManage;

@Service("customAllotConfigService")
public class CustomAllotConfigServiceImpl implements CustomAllotConfigService {

	@Autowired
	private CustomAllotConfigDao customAllotConfigDao;
	private SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	//通过部门id查询该部门的分配轮循设置
	@Override
	public List<CustomAllotConfig> showAllotConfigByDeptno(String deptno) throws SQLException {
		List<CustomAllotConfig> configList=customAllotConfigDao.showAllotConfigByDeptno(deptno);
		if(configList.isEmpty())
			return null;
		return configList;
	}

	//显示客户分配设置页面 部门 
	@Override
	public PageView<CustomAllotConfigDept> queryAllotConfig(PageView<CustomAllotConfigDept> pageView) throws SQLException {
		pageView=customAllotConfigDao.queryAllotConfig(pageView);
		if(pageView.getRecords().isEmpty())
			return null;
		return pageView;
	}

	//用组织结构标识和上级组织结构标识查询分配设置页面部门以及所属分公司
	@Override
	public PageView<CustomAllotConfigDept> queryAllotConfig(PageView<CustomAllotConfigDept> pageView,CustomAllotConfigDept configDept) throws SQLException {
		pageView=customAllotConfigDao.queryAllotConfig(pageView,configDept);
		if(pageView.getRecords().isEmpty())
			return null;
		return pageView;
	}

	//修改部门分配轮循设置
	@Override
	public void updateAllotConfig(CustomAllotConfig allotConfig) throws SQLException {
		customAllotConfigDao.updateAllotConfig(allotConfig);
	}

	//根据部门名称过滤查询分配设置列表
	@Override
	public PageView<CustomAllotConfigDept> queryAllotDeptByDname(PageView<CustomAllotConfigDept> pageView,Dept dept) throws SQLException {
		return customAllotConfigDao.queryAllotDeptByDname(pageView, dept);
	}

	//批量修改部门分配轮循设置
	@Override
	public void updateAllotConfigBatch(String deptnos, String dates) throws SQLException {
		//取得前端设置的部门id集 和每个部门设置的轮循时间集  将拼接字符串分割成正常时间格式
		String[] deptno=deptnos.split(":");
		String[] configDates=dates.split("##");
		/**批量设置的部门个数循环*/
		for(int i=0;i<deptno.length;i++) {
			/**批量设置的工作日循环*/
			for(int j=0;j<configDates.length;j++) {
				CustomAllotConfig allotConfig=new CustomAllotConfig();
				allotConfig.setDeptno(deptno[i]);
				String[] date=configDates[j].split("!");
				allotConfig.setStartTime(date[0]);
				allotConfig.setEndTime(date[1]);
				allotConfig.setPollInterval(Integer.parseInt(date[2]));
				allotConfig.setCheckResult(CustomerManage.USE_NO);
				switch(j) {
					case 0:allotConfig.setWorkDate("星期一");break;
					case 1:allotConfig.setWorkDate("星期二");break;
					case 2:allotConfig.setWorkDate("星期三");break;
					case 3:allotConfig.setWorkDate("星期四");break;
					case 4:allotConfig.setWorkDate("星期五");break;
					case 5:allotConfig.setWorkDate("星期六");break;
					case 6:allotConfig.setWorkDate("星期日");break;
				}
				allotConfig.setUpdateDate(sf.format(new Date()));
				customAllotConfigDao.updateAllotConfig(allotConfig);
				customAllotConfigDao.updateAllotConfigCheckResult(allotConfig);
			}
		}
	}

	//单独修改部门分配轮循设置
	@Override
	public void updateAllotConfigAlong(String deptno, String startTime,String endTime, String poll, String workDate) throws SQLException {
		CustomAllotConfig allotConfig=new CustomAllotConfig();
		allotConfig.setDeptno(deptno);
		allotConfig.setStartTime(startTime);
		allotConfig.setEndTime(endTime);
		allotConfig.setPollInterval(Integer.parseInt(poll));
		allotConfig.setCheckResult(CustomerManage.USE_NO);
		allotConfig.setUpdateDate(sf.format(new Date()));
		switch (Integer.parseInt(workDate)) {
			case 0:allotConfig.setWorkDate("星期一");break;
			case 1:allotConfig.setWorkDate("星期二");break;
			case 2:allotConfig.setWorkDate("星期三");break;
			case 3:allotConfig.setWorkDate("星期四");break;
			case 4:allotConfig.setWorkDate("星期五");break;
			case 5:allotConfig.setWorkDate("星期六");break;
			case 6:allotConfig.setWorkDate("星期日");break;
		}
		customAllotConfigDao.updateAllotConfig(allotConfig);
		customAllotConfigDao.updateAllotConfigCheckResult(allotConfig);
	}

	/**添加部门时增加初始化客户分配规则*/
	@Override
	public void addDeptDsitributRule(String deptno) throws SQLException {
		//增加周一至周五的初始化规则
		for(int i=0;i<7;i++) {
			CustomAllotConfig allotConfig=new CustomAllotConfig();
			allotConfig.setId(UUID.randomUUID().toString());
			allotConfig.setDeptno(deptno);
			allotConfig.setStartTime("07:30");
			allotConfig.setEndTime("18:30");
			//如果是周六日 设置轮循时间为0 表示不轮循
			if(i>4) {
				allotConfig.setPollInterval(0);
			} else {
				allotConfig.setPollInterval(15);
			}
			allotConfig.setCheckResult(1);
			allotConfig.setUpdateDate(sf.format(new Date()));
			switch(i) {
				case 0:allotConfig.setWorkDate("星期一");allotConfig.setWorkDateNum(1);break;
				case 1:allotConfig.setWorkDate("星期二");allotConfig.setWorkDateNum(2);break;
				case 2:allotConfig.setWorkDate("星期三");allotConfig.setWorkDateNum(3);break;
				case 3:allotConfig.setWorkDate("星期四");allotConfig.setWorkDateNum(4);break;
				case 4:allotConfig.setWorkDate("星期五");allotConfig.setWorkDateNum(5);break;
				case 5:allotConfig.setWorkDate("星期六");allotConfig.setWorkDateNum(6);break;
				case 6:allotConfig.setWorkDate("星期日");allotConfig.setWorkDateNum(7);break;
				default:return;
			}
			customAllotConfigDao.addDeptDsitributRule(allotConfig);
		}
	}
	
	/**
	 * 获取客户分配规则
	 * @param deptIdStr
	 * @return
	 */
	public List<CustomAllotConfigDept> getCustomDsitributRuleByDeptIdStr(String deptIdStr){
		
		List<CustomAllotConfigDept> customAllotConfigDeptList = customAllotConfigDao.getCustomDsitributRuleByDeptIdStr(deptIdStr);
		return customAllotConfigDeptList;
	}
	
	/**
	 * 获取客户分配规则
	 * @param dname
	 * @return
	 */
	public List<CustomAllotConfigDept> getCustomDsitributRuleByDname(String dname){
		List<CustomAllotConfigDept> customAllotConfigDeptList = customAllotConfigDao.getCustomDsitributRuleByDname(dname);
		return customAllotConfigDeptList;
	}

}
